﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Hotel.Classes.Helper;
using Hotel.Models.Entity;
using Hotel.Models.Services;

namespace Hotel.Models.DataAccess
{
    public class DAService
    {

        public static List<Service> GetListServiceByGroupID(int groupServiceID)
        {
            DataTable dt;
            List<Service> list = new List<Service>();
            using (DBManager dbManager = new DBManager("stp_Get_Service_By_FKGroupService"))
            {
                dbManager.Add("p_FK_GroupServiceID", groupServiceID);
                dt = dbManager.GetDatatable();
            }
            foreach (DataRow dr in dt.Rows)
            {
                Service service = new Service();
                service.PkServiceId = DataHelper.GetInt(dr, "PK_ServiceID");
                GroupService grpSrvc =
                GroupServiceService.GetGroupServiceDetail(DataHelper.GetInt(dr, "FK_GroupServiceID"));
                service.GroupService = grpSrvc;
                service.Name = DataHelper.Get(dr, "Name");
                service.Units = DataHelper.Get(dr, "Units");
                service.Entered = DataHelper.GetInt(dr, "Entered");
                service.Instock = DataHelper.GetInt(dr, "Instock");
                service.Outstock = DataHelper.GetInt(dr, "Outstock");
                service.Price = DataHelper.GetDouble(dr, "Price");
                service.Price = DataHelper.GetDouble(dr, "Price");
                service.Description = DataHelper.Get(dr, "Description");
                service.IsInOut = DataHelper.GetBool(dr, "IsInOut");
                list.Add(service);
            }
            return list;
        }

        public static List<Service> GetListService(int serviceID)
        {
            DataTable dt;
            List<Service> list = new List<Service>();
            using (DBManager dbManager = new DBManager("stp_Get_Service"))
            {
                dbManager.Add("p_PK_ServiceID", serviceID);
                dbManager.Add("IsDelete", 0);
                dt = dbManager.GetDatatable();
            }
            foreach (DataRow dr in dt.Rows)
            {
                Service service = new Service();
                service.PkServiceId = DataHelper.GetInt(dr, "PK_ServiceID");
                GroupService grpSrvc =
                GroupServiceService.GetGroupServiceDetail(DataHelper.GetInt(dr, "FK_GroupServiceID"));
                service.GroupService = grpSrvc;
                service.Name = DataHelper.Get(dr, "Units");
                service.Units = DataHelper.Get(dr, "Amenities");
                service.Entered = DataHelper.GetInt(dr, "Entered");
                service.Instock = DataHelper.GetInt(dr, "Instock");
                service.Outstock = DataHelper.GetInt(dr, "Outstock");
                service.Price = DataHelper.GetDouble(dr, "Price");
                service.Price = DataHelper.GetDouble(dr, "Price");
                service.Description = DataHelper.Get(dr, "Description");
                service.IsInOut = DataHelper.GetBool(dr, "IsInOut");
                list.Add(service);
            }
            return list;
        }

        public static bool DeleteService(int serviceID)
        {
            using (DBManager dbManager = new DBManager("stp_Get_Service"))
            {
                try
                {
                    dbManager.Add("PK_ServiceID", serviceID);
                    dbManager.Add("IsDelete", 1);
                    dbManager.ExecuteNonQuery();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public static bool SaveService(Service service, bool isUpdate)
        {
            using (DBManager dbManager = new DBManager("stp_Create_Service"))
            {
                try
                {
                    dbManager.Add("p_PK_ServiceID", service.PkServiceId);
                    dbManager.Add("p_FK_GroupServiceID", service.GroupService.PkGroupService);
                    dbManager.Add("p_Name", service.Name);
                    dbManager.Add("p_Price", service.Price);
                    dbManager.Add("p_Description", service.Description);
                    dbManager.Add("p_Units", service.Units);
                    dbManager.Add("p_IsInOut", service.IsInOut);
                    dbManager.Add("IsUpdate", isUpdate);
                    dbManager.ExecuteNonQuery();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
    }
}